<template>
  <div o-Container>
    <o-list v-bind:source="list.dictionary"
            v-bind="{created:create,modified:modify}"/>
    <o-dialog v-bind:source="panel" v-bind="{opened:load,closed:load}">
      <template slot="content">
        <o-item v-bind:source="item.dictionary" v-bind="{saved:save,backed:back,changed:change,validated: validate}"></o-item>
      </template>
    </o-dialog>
  </div>
</template>

<script>
  export default {
    name: "mms_data_dictionary_list", /* 这个name暂时不知道用啥用，根据官方文档说的是方便排错的 */
    data() {
      return {
        octopus: this.$parent.octopus,
        list: {
          dictionary: {
            data: {
              id: 183, // 菜单表的八爪鱼ID
              cellData: [
                {name: "ID", width: 50},
                {name: "Code", searchKey: "LIKE", width: 100},
                {name: "ParentKeyValue", searchKey: "=", width: 100},
                {name: "KeyValue", searchKey: "=", width: 100},
                {name: "DataValue", searchKey: "LIKE", width: 200},
                {name: "Comment", searchKey: "LIKE", width: 300},
                {name: "CreateDate", width: 160},
              ],
            }
          },
        },
        item: {
          dictionary: {
            // 以下参数默认由组件内部创建，但是如果有需要，开发人员可在这里提前设置，组件将按照您的预期渲染。
            // cache: false,
            // control: null,
            // dataExtend: null,
            data: {
              id: 183,
              value: "",
              cellData: [
                {name: "Code"},
                {name: "ParentKeyValue"},
                {name: "KeyValue"},
                {name: "DataValue"},
                // {name: "CreateDate"},
                {name: "Comment"},
              ]
            },
          },
        },
        panel: {
          visible: false,
        },
        KeyValueArr:[]
      }
    },
    methods: {
      create: function () {
        this.item.dictionary.data.value = null;
        this.item.dictionary.control.load(); //刷新
        this.panel.visible = true;//打开
      },
      modify: function (row) {
        // 加载对应的列关联绑定数据
        this.item.dictionary.data.value = row.ID;
        this.item.dictionary.control.load();
        this.panel.visible = true; //打开
      },
      validate(status) {
        if (status) {//默认校验通过
          if(this.KeyValueArr.length>0){
            for (let s of this.KeyValueArr) {
              if(s.Code== this.octopus.tools.parseCell(this.item.dictionary.data, "Code").defaultValue){//同一个Code，不能有相同的KeyValue
                this.octopus.tips.error('键已存在，请重新输入');
                return false;
              }
            }
          }
        }
        return status;
      },
      change(data){//同一个Code，不能有相同的KeyValue
        if (data.name == "KeyValue"){
          // if(this.initCode==data.defaultValue){//编辑初始值 和 后来改变值相等；说明编辑时候没有改变Code字段的值，所以不用查询了
          //   this.hasCode=false;
          //   return
          // }
          this.octopus.request.api.post("/Octopus/Engine/List", {
              id: 183,
              cellData: [{
                  name: 'Code'
                },
                {
                  name: "KeyValue",
                  filterKey: "=",
                  filterValue: data.defaultValue
                }
              ]
            },
            (data)=> {
              if (data.status.code == 200 && data.content.queryData.length > 0) {
                this.KeyValueArr=data.content.queryData;
              }
              else{
                this.KeyValueArr=[];
              }
            })
        }
      },
      save: function () { //同一个Code，不能有相同的KeyValue
        // 刷新list
        this.list.dictionary.control.load(); //刷新
        this.back(); //返回
      },
      back: function () {
        this.panel.visible = false; //取消
      }
    },
    props: ["oData"] //数据
  }
</script>
